package org.mybatis.generator.plugins;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description: 对生成的model进行操作：引入lombok插件，不生成getter/setter
 * @author: 01369542
 * @create: 2018-07-05
 */
public class MyModelPlugin extends PluginAdapter {

    /*
     * @Description: 继承PluginAdapter必选
     * @Author: 01369542
     * @Date: 2018/7/18
     */
    @Override
    public boolean validate(List<String> list) {
        return true;
    }

    /*
     * @Description: 对生成的model实体类进行加工
     * @Author: 01369542
     * @Date: 2018/7/18
     */
    @Override
    public boolean modelBaseRecordClassGenerated(
            TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

        // 添加model的import
        topLevelClass.addImportedType("javax.persistence.Id");
        topLevelClass.addImportedType("javax.persistence.GeneratedValue");
        topLevelClass.addImportedType("javax.persistence.GenerationType");
        topLevelClass.addImportedType("javax.persistence.Table");
        topLevelClass.addImportedType("lombok.Data");
        topLevelClass.addImportedType("lombok.ToString");
        topLevelClass.addImportedType("lombok.NoArgsConstructor");
        topLevelClass.addImportedType("org.codehaus.jackson.annotate.JsonIgnoreProperties");
        topLevelClass.addImportedType("io.swagger.annotations.ApiModelProperty");

        // 添加model的注解
        topLevelClass.addAnnotation("@Data");
        topLevelClass.addAnnotation("@ToString");
        topLevelClass.addAnnotation("@NoArgsConstructor");
        topLevelClass.addAnnotation("@JsonIgnoreProperties(ignoreUnknown = true)");
        topLevelClass.addAnnotation("@Table(name = \"" + introspectedTable.getFullyQualifiedTable() + "\")");

        // 添加domain的注释；可以在这里写domain的类注释，也可以在DefaultCommentGenerator中的addClassComment方法里面写
        topLevelClass.addJavaDocLine("/**");
        topLevelClass.addJavaDocLine(
                " * @Description "
                        + introspectedTable.getFullyQualifiedTable().getRemarks()
                        + " 的实体类，对应数据库中的表为："
                        + introspectedTable.getFullyQualifiedTable());
        topLevelClass.addJavaDocLine(" * @Author 01369542");
        topLevelClass.addJavaDocLine(" * @Date " + new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        topLevelClass.addJavaDocLine(" */");

        return true;
    }


    /*
     * @Description: 不生成setter方法
     * @Author: 01369542
     * @Date: 2018/7/18
     */
    @Override
    public boolean modelSetterMethodGenerated(
            Method method,
            TopLevelClass topLevelClass,
            IntrospectedColumn introspectedColumn,
            IntrospectedTable introspectedTable,
            ModelClassType modelClassType) {
        return false;
    }

    /*
     * @Description: 不生成getter方法
     * @Author: 01369542
     * @Date: 2018/7/18
     */
    @Override
    public boolean modelGetterMethodGenerated(
            Method method,
            TopLevelClass topLevelClass,
            IntrospectedColumn introspectedColumn,
            IntrospectedTable introspectedTable,
            ModelClassType modelClassType) {
        return false;
    }

}
